package br.ucb.sigplan.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.ucb.sigplan.beans.Login;
import br.ucb.sigplan.beans.Professor;

import br.ucb.sigplan.connection.FactoryCon;
import br.ucb.sigplan.dao.ProfessorDAO;
import br.ucb.sigplan.enuns.EnumTipoUsuario;
import br.ucb.sigplan.exception.DAOException;

public class ProfessorDAOImpl implements ProfessorDAO{
	private final String SQL_INSERT = "insert into professor (nome,matricula,senha,email,tipo) values (?,?,?,?,?)";
	private final String SQL_FINDALL = "select * from professor";
	private final String SQL_SEARCHBYID = "select * from professor where idprofessor = ?";
	private final String SQL_SEARCHBYLOGIN = "select * from professor where email = ? and senha = ? and tipo = ?";
	private final String SQL_SEARCHBYEMAIL = "select * from professor where email = ?";
	private final String SQL_DELETE = "delete from professor where idprofessor = ?";
	private final String SQL_UPDATE = "update professor set nome = ?, matricula = ?, senha =?, email = ?, tipo = ? where idprofessor = ?";
	private final String SQL_UPDATESENHA = "update professor set senha = ? where email= ? and matricula like ? ;"; 
	
	public List<Professor> findAll() throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		ResultSet set = null;
		List<Professor> professores = new ArrayList<Professor>();
		Professor professor;
		try {
			instrucao = con.prepareStatement(SQL_FINDALL);
			set = instrucao.executeQuery();
			while (set.next()) {
				professor = new Professor();
				putProfessor(set, professor);
				professores.add(professor);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao,set);
		}
		return professores;
	}
	
	public void putProfessor(ResultSet set, Professor professor) throws SQLException {
		professor.setIdProfessor(set.getInt("idprofessor"));
		professor.setNome(set.getString("nome"));
		professor.setMatricula(set.getString("matricula"));
		professor.setSenha(set.getString("senha"));
		professor.setEmail(set.getString("email"));
		professor.setTipo(EnumTipoUsuario.findByValue(set.getString("tipo").charAt(0)));
	}

	public void insert(Professor professor) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_INSERT);
			instrucao.setString(1, professor.getNome());
			instrucao.setString(2, professor.getMatricula());
			instrucao.setString(3, professor.getSenha());
			instrucao.setString(4, professor.getEmail());
			instrucao.setString(5, professor.getTipo().getValue().toString());
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroInsercao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
		
	}

	public Professor findById(Integer idProfessor) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		ResultSet set = null;
		Professor professor = null;
		try {
			instrucao = con.prepareStatement(SQL_SEARCHBYID);
			instrucao.setInt(1, idProfessor);
			set = instrucao.executeQuery();
			while (set.next()) {
				professor = new Professor();
				putProfessor(set, professor);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao, set);
		}
		return professor;
	}

	public void delete(Integer idprofessor) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_DELETE);
			instrucao.setInt(1, idprofessor);
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("professorDelecao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
	}
	

	public Professor findByLogin(Login login) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		ResultSet set = null;
		Professor professor = null;
		try {
			instrucao = con.prepareStatement(SQL_SEARCHBYLOGIN);
			instrucao.setString(1, login.getEmail());
			instrucao.setString(2, login.getSenha());
			instrucao.setString(3, login.getTipo().getValue().toString());
			set = instrucao.executeQuery();
			while (set.next()) {
				professor = new Professor();
				putProfessor(set, professor);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao, set);
		}
		return professor;
	}

	@Override
	public void update(Professor professor) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_UPDATE);
			instrucao.setString(1, professor.getNome());
			instrucao.setString(2, professor.getMatricula());
			instrucao.setString(3, professor.getSenha());
			instrucao.setString(4, professor.getEmail());
			instrucao.setString(5, professor.getTipo().getValue().toString());
			instrucao.setInt(6, professor.getIdProfessor());
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroAtualizacao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
		
	}

	@Override
	public Professor findByEmail(String email) {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		ResultSet set = null;
		Professor professor = null;
		try {
			instrucao = con.prepareStatement(SQL_SEARCHBYEMAIL);
			instrucao.setString(1, email);
			set = instrucao.executeQuery();
			while (set.next()) {
				professor = new Professor();
				putProfessor(set, professor);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao, set);
		}
		return professor;
	}


	@Override
	public void updateSenha(String senha, String email, String parteMatricula) {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_UPDATESENHA);
			instrucao.setString(1, senha);
			instrucao.setString(2, email);
			instrucao.setString(3, parteMatricula+"%");	
			System.out.println(instrucao.toString());
			if (instrucao.executeUpdate() == 0)
				throw new DAOException("erroRestaurarSenhaJaAlterada");
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroRestaurarSenha");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
		
	}
	
}
